//
//  WXPAuthPersonalContentFooterView.m
//  MexicoLoan
//
//  Created by WXP on 2025/3/2.
//
//

#import "WXPAuthPersonalContentFooterView.h"
#import "WXPDatePickerView.h"

@implementation WXPAuthPersonalContentFooterView

#pragma mark - lazy
- (UIView *)viewContent{
    if (!_viewContent) {
        _viewContent = [UIView new];
        _viewContent.backgroundColor = COLOR_WHITE;
        _viewContent.width = SCREEN_WIDTH-LEFT_MARDIN*2;
    }
    return _viewContent;
}
- (UIView *)viewEmailErroeHint
{
    if (_viewEmailErroeHint == nil) {
        _viewEmailErroeHint = [UIView  new];
        _viewEmailErroeHint.backgroundColor = [UIColor clearColor];
        _viewEmailErroeHint.widthHeight = XY(self.viewContent.width, 0);
        [_viewEmailErroeHint addSubview:self.labelEmailErroeHint];
    }
    return  _viewEmailErroeHint;
}
- (UILabel *)labelEmailErroeHint
{
    if (_labelEmailErroeHint == nil) {
        _labelEmailErroeHint = [UILabel  new];
        _labelEmailErroeHint.fontNum = 12;
        _labelEmailErroeHint.numberOfLines = 0;
        _labelEmailErroeHint.lineSpace = 4;
        _labelEmailErroeHint.textColor = COLOR_RED;
    }
    return  _labelEmailErroeHint;
}

// 婚姻
- (WXPAuthSelectView *)maritalView
{
    if (_maritalView == nil) {
        _maritalView = [WXPAuthSelectView  new];
        ModelBtn * model = [ModelBtn new];
        model.title = @"Estado Civil";
        [_maritalView resetViewWithModel:model];
        WEAKSELF
        _maritalView.blockSelect = ^{
            [weakSelf endEditing:true];
            [GlobalMethod buryPointWithPageName:KEY_VerifyPerson featureName:KEY_Civil_Status action:KEY_GET_FOCUS type:KEY_select];
            [GlobalMethod hitPointWithPageName:KEY_VerifyPerson action:KEY_Civil_Status_Focus extInfo:nil];
            NSMutableArray * array = [NSMutableArray new];
            for (int i = 0; i < weakSelf.maritalAry.count; i ++) {
                NSDictionary * dic = weakSelf.maritalAry[i];
                ModelBtn * model = [ModelBtn new];
                model.title = [dic stringValueForKey:KEY_remark];
                model.content = [dic stringValueForKey:KEY_typeName];
                [array addObject:model];
            }
            [weakSelf.pickerView resetViewWithAry:array withTag:2];
            UIWindow * window = [[[UIApplication sharedApplication] delegate] window];
            [window endEditing:true];
            [window addSubview:weakSelf.pickerView];
        };
    }
    return  _maritalView;
}

// 性别
- (WXPAuthSelectView *)sexView
{
    if (_sexView == nil) {
        _sexView = [WXPAuthSelectView  new];
        ModelBtn * model = [ModelBtn new];
        model.title = @"Género";
        [_sexView resetViewWithModel:model];
        WEAKSELF
        _sexView.blockSelect = ^{
            [weakSelf endEditing:true];
            [GlobalMethod buryPointWithPageName:KEY_VerifyPerson featureName:KEY_Gender action:KEY_GET_FOCUS type:KEY_select];
            [GlobalMethod hitPointWithPageName:KEY_VerifyPerson action:KEY_Gender_Focus extInfo:nil];
            NSMutableArray * array = [NSMutableArray new];
            for (int i = 0; i < weakSelf.sexAry.count; i ++) {
                NSDictionary * dic = weakSelf.sexAry[i];
                ModelBtn * model = [ModelBtn new];
                model.title = [dic stringValueForKey:KEY_remark];
                model.content = [dic stringValueForKey:KEY_typeName];
                [array addObject:model];
            }
            [weakSelf.pickerView resetViewWithAry:array withTag:3];
            UIWindow * window = [[[UIApplication sharedApplication] delegate] window];
            [window endEditing:true];
            [window addSubview:weakSelf.pickerView];
        };
    }
    return  _sexView;
}

// 出生日期
- (WXPAuthSelectView *)birthView
{
    if (_birthView == nil) {
        _birthView = [WXPAuthSelectView  new];
        ModelBtn * model = [ModelBtn new];
        model.title = @"Fecha de nacimiento";
        [_birthView resetViewWithModel:model];
        WEAKSELF
        _birthView.blockSelect = ^{
            [weakSelf endEditing:true];
            [GlobalMethod buryPointWithPageName:KEY_VerifyPerson featureName:KEY_Birthday action:KEY_GET_FOCUS type:KEY_select];
            [GlobalMethod hitPointWithPageName:KEY_VerifyPerson action:KEY_Birthday_Focus extInfo:nil];
            NSInteger year = [[GlobalMethod exchangeDate:[NSDate date] formatter:@"YYYY"] integerValue];
            NSCalendar *calendar = [NSCalendar currentCalendar];
            NSDate *currentDate = [NSDate date];
            NSDateComponents *minAgeComponents = [[NSDateComponents alloc] init];
            [minAgeComponents setYear:-45];
            NSDate *minDate = [calendar dateByAddingComponents:minAgeComponents toDate:currentDate options:0];

            NSDateComponents *maxAgeComponents = [[NSDateComponents alloc] init];
            [maxAgeComponents setYear:-18];
            NSDate *maxDate = [calendar dateByAddingComponents:maxAgeComponents toDate:currentDate options:0];
            NSDate *scrollToDate = maxDate;
            if(isStr(self.birthView.tfContent.text)){
                scrollToDate = [GlobalMethod exchangeString:self.birthView.tfContent.text formatter:TIME_DAY_MONTH_YEAR];
            }
            WXPDatePickerView * datepicker = [[WXPDatePickerView alloc] initWithDateStyle:DateStyleShowDayMonthYear scrollToDate:scrollToDate maxYear:year-18 minYear:year-45 CompleteBlock:^(NSDate *selectDate) {
                //选择的时间
                NSString * timeStr = [selectDate stringWithFormat:TIME_DAY_MONTH_YEAR];
                weakSelf.birthView.tfContent.text = timeStr;
                 [GlobalMethod buryPointWithPageName:KEY_VerifyPerson featureName:KEY_Birthday action:KEY_LOST_FOCUS type:KEY_select];
                [weakSelf resetEntregarBtn];
            }];
            datepicker.blockDismiss = ^{
                [GlobalMethod buryPointWithPageName:KEY_VerifyPerson featureName:KEY_Birthday action:KEY_LOST_FOCUS type:KEY_select];
            };

            datepicker.maxLimitDate = maxDate;
            datepicker.minLimitDate = minDate;
            datepicker.dateLabelColor = [UIColor blackColor]; //年-月-日-时-分 颜色
            datepicker.datePickerColor = [UIColor blackColor]; //滚轮日期颜色
            [datepicker show];
        };
    }
    return  _birthView;
}

- (WXPAuthPickerView *)pickerView
{
    if (_pickerView == nil) {
        _pickerView = [WXPAuthPickerView  new];
        WEAKSELF
        _pickerView.blockDidSelectRow = ^(ModelBtn *model, NSInteger tag) {
            if(tag == 2){
                [GlobalMethod buryPointWithPageName:KEY_VerifyPerson featureName:KEY_Civil_Status action:KEY_LOST_FOCUS type:KEY_select];
                if(model != nil){
                    weakSelf.maritalView.tfContent.text = model.title;
                    weakSelf.maritalStatus = model.content;
                }
            }else if(tag == 3){
                [GlobalMethod buryPointWithPageName:KEY_VerifyPerson featureName:KEY_Gender action:KEY_LOST_FOCUS type:KEY_select];
                if(model != nil){
                    weakSelf.sexView.tfContent.text = model.title;
                    weakSelf.gender = model.content;
                }
            }
            [weakSelf resetEntregarBtn];
        };
    }
    return  _pickerView;
}


#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.width = SCREEN_WIDTH;
        [self configUI];
    }
    return self;
}

- (void)configUI{
    [self.viewContent removeFromSuperview];
    self.viewContent = [UIView initWithViews:@[
        self.isShowEmailError ? self.viewEmailErroeHint : [UIView new],
        self.maritalView,
        self.sexView,
        self.birthView,
        [GlobalMethod getViewWithHeight:Ratio(20)],
    ] width:SCREEN_WIDTH-2*LEFT_MARDIN];
    self.viewContent.backgroundColor = COLOR_WHITE;
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.viewContent.bounds byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(16, 16)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.path = path.CGPath;
    self.viewContent.layer.mask = maskLayer;
    self.viewContent.centerXTop = XY(self.width/2, 0);
    [self addSubview:self.viewContent];
    self.height = self.viewContent.bottom+Ratio(20);
}

- (void)resetEmailHint:(NSString *)emailStr{
    BOOL endsWithSuffix = [GlobalMethod isValidEmail:emailStr];
    BOOL haveSpace = ([emailStr rangeOfString:@" "].location != NSNotFound);
    if(haveSpace){
        [self.labelEmailErroeHint fitTitle:@"En su dirección de correo hay [espacio], por favor modifíquelo" variable:(self.viewContent.width-LEFT_MARDIN*2)];
    }else{
        if(endsWithSuffix == false){
            [self.labelEmailErroeHint fitTitle:@"Error de formato del correo electrónico" variable:self.viewContent.width-LEFT_MARDIN*2];
        }
    }
    if(isStr(emailStr)){
        if(endsWithSuffix && haveSpace == false){
            if(self.isShowEmailError == true){
                self.isShowEmailError = false;
                [self resetHeaderView];
            }
        }else{
            // 邮箱格式错误
            if(self.isShowEmailError == false){
                self.isShowEmailError = true;
                self.labelEmailErroeHint.leftTop = XY(LEFT_MARDIN+2, Ratio(16));
                self.viewEmailErroeHint.height = self.labelEmailErroeHint.bottom;
                [self resetHeaderView];
            }
        }
    }else{
        // 没有输入，就不提示
        if(self.isShowEmailError == true){
            self.isShowEmailError = false;
            [self resetHeaderView];
        }
    }
}

- (void)resetHeaderView{
    [self configUI];
    if(self.blockResetFooter){
        self.blockResetFooter();
    }
}

// 提交按钮 - 状态更新
- (void)resetEntregarBtn{
    if(self.blockResetAuthBtn){
        self.blockResetAuthBtn();
    }
}



@end
